<?php

/**
 * This is the model class for table "programok".
 *
 * The followings are the available columns in table 'programok':
 * @property integer $id
 * @property string $nev
 * @property string $leiras
 * @property string $datum
 * @property string $address
 * @property string $icon
 */
class Program extends CActiveRecord
{
    public $image;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Program the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'programok';
	}
    
    public function behaviors() {
		return array(
			'imageBehavior' => array(
				'class' => 'ImageARBehavior',
				'attribute' => 'image', // this must exist
				'extension' => 'png, jpg', // possible extensions, comma separated
				'prefix' => 'event_',
				'relativeWebRootFolder' => 'images/events', // this folder must exist
				
				# 'forceExt' => png, // this is the default, every saved image will be a png one.
				# Set to null if you want to keep the original format
				
				//'useImageMagick' => '/usr/bin', # I want to use imagemagick instead of GD, and
				# it is located in /usr/bin on my computer.
				
				// this will define formats for the image.
				// The format 'normal' always exist. This is the default format, by default no
				// suffix or no processing is enabled.
				'formats' => array(
					// create a thumbnail grayscale format
					'thumb' => array(
						'suffix' => '_thumb',
						'process' => array('resize' => array(60, 60)),
					),
					// create a large one (in fact, no resize is applied)
					'large' => array(						
                        'process' => array('resize' => array(1000, 1000)),
					),
					// and override the default :
					'normal' => array(
                        'suffix' => '_mid',
						'process' => array('resize' => array(200, 200)),
					),
				),
				
				'defaultName' => 'default', // when no file is associated, this one is used by getFileUrl
				// defaultName need to exist in the relativeWebRootFolder path, and prefixed by prefix,
				// and with one of the possible extensions. if multiple formats are used, a default file must exist
				// for each format. Name is constructed like this :
				//     {prefix}{name of the default file}{suffix}{one of the extension}
			)
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nev, leiras, datum, address', 'required'),
			array('nev', 'length', 'max'=>100),
            array('leiras', 'length', 'min'=>50),
            array('address', 'length', 'max'=>255),
            array('icon', 'length', 'max'=>500),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, nev, leiras, datum', 'safe', 'on'=>'search'),
            array('image', 'file', 'types' => 'png, jpg', 'allowEmpty' => true), // behavior
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nev' => 'Cím',
			'leiras' => 'Leírás',
			'datum' => 'Dátum',
            'address'=> 'Hely',
            'icon'=>'Ikon',
		);
	}
    
    public function getIconUrl(){
        return Yii::app()->baseUrl.'/images/mapicons/'.$this->icon;
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nev',$this->nev,true);
		$criteria->compare('leiras',$this->leiras,true);
		$criteria->compare('datum',$this->datum,true);
        $criteria->compare('address',$this->address,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}